package org.k.reloadable.demo.conf;

import org.k.reloadable.core.Reloadable;
import org.k.reloadable.core.cache.ICache;
import org.k.reloadable.core.cache.impl.MemoryCache;
import org.k.reloadable.core.client.ConfClient;
import org.k.reloadable.core.client.IEventPublisher;
import org.k.reloadable.core.client.ZKEventPublisher;
import org.k.reloadable.spring.ReadablePropertySourcesPlaceholderConfigurer;
import org.k.reloadable.spring.ReloadablePropertyPostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by hengxianwang on 16-12-21.
 */
@Configuration
public class ReloadableConf {

    @Bean
    public static ConfClient confClient(){
        ConfClient confClient = new ConfClient();
        confClient.setCache(new MemoryCache());
        return confClient;
    }

    @Bean
    public static ReadablePropertySourcesPlaceholderConfigurer ReadablePropertySourcesPlaceholderConfigurer(ConfClient confClient){
        ReadablePropertySourcesPlaceholderConfigurer configurer = new ReadablePropertySourcesPlaceholderConfigurer();
        configurer.setConfClient(confClient);
        return configurer;
    }

    @Bean
    public ReloadablePropertyPostProcessor reloadablePropertyPostProcessor(){
        return new ReloadablePropertyPostProcessor();
    }


    @Bean
    public Reloadable getReloadable(ConfClient confClient, ReloadablePropertyPostProcessor reloadablePropertyPostProcessor){
        Reloadable reloadable = new Reloadable();
        reloadable.addObserver(confClient);
        reloadable.addObserver(reloadablePropertyPostProcessor);
        return reloadable;
    }

    @Bean
    public IEventPublisher getEventPublisher(Reloadable reloadable){
        ZKEventPublisher publisher = new ZKEventPublisher();
        publisher.setReloadable(reloadable);
        return publisher;
    }
}
